import UnexecuteExpandRow from './UnexecuteExpandRow.vue'
import ROUTER from '@/router/router'
import { setEntryManuEffect } from '@/utils/storage.js'

export const createDiectiveFactor = () => [
  [
    { label: '指令编号', value: '', field: 'ident' },
    { label: '总到达率', value: '', field: 'finishRate' },
  ],
  [
    { label: '指令下达时间', value: '', field: 'startTime' },
    { label: '指令截止时间', value: '', field: 'endTime' },
  ],
  [
    { label: '推送范围', value: '', field: 'pushScope' },
    { label: '指令状态', value: '', field: 'state.value' },
  ],
  // articleCount 只有稿件转发有 refreshCount 开机屏指令有
  // 前端无法确定是哪个指令，判断 值为 null 时，不显示
  [
    { label: '刷出次数（次）', value: null, field: 'refreshCount' },
    { label: '稿件总量', value: null, field: 'articleCount' },
  ],
]

export const createColumns_overview = (vm) => [
  { title: '序号', type: 'index', align: 'center', width: 80 },
  /*  {
    title: '',
    align: 'center',
    width: 300,
  },
  {
    title: '',
    key: '',
    align: 'center',
    width: 300,
  }, */
  { title: '到达率%', key: 'finishRate', align: 'center' },
  { title: '应执行', key: 'count', align: 'center' },
  { title: '已执行', key: 'exec', align: 'center' },
  { title: '未执行', key: 'notExec', align: 'center' },
]

export const createColumns_websiteExecute = (vm) => [
  { title: '序号', type: 'index', align: 'center', width: 70 },
  /* { title: '稿件标题', key: '', align: 'center' },
  { title: '执行网站', key: '', align: 'center' },
  { title: '稿发时间', key: '', align: 'center' },
  { title: '发布位置', key: '', align: 'center' },
  { title: '上稿时长', key: '', align: 'center' }, */
  {
    title: '是否合规',
    align: 'center',
    children: [
      {
        title: '\\',
        width: 50,
        align: 'center',
        render(h, params) {
          const valid = params.row.valid
          return <span class={[valid ? 'text-sueecss' : 'text-highlight']}>{valid ? '是' : '否'}</span>
        },
      },
      {
        title: '未在指定位置',
        align: 'center',
        render(h, params) {
          return params.row.notLocation && <icon class="f24" type="md-checkmark"></icon>
        },
      },
      {
        title: '超时发文',
        align: 'center',
        render(h, params) {
          return params.row.timeout && <icon class="f24" type="md-checkmark"></icon>
        },
      },
      {
        title: '时长不足',
        align: 'center',
        render(h, params) {
          return params.row.dateLenLess && <icon class="f24" type="md-checkmark"></icon>
        },
      },
    ],
  },
]

export const createColumns_unexecute = (vm) => [
  {
    type: 'expand',
    width: 50,
    render: (h, params) => {
      return h(UnexecuteExpandRow, {
        props: {
          row: params.row,
          columns: vm.unexecute_expand_columns,
        },
      })
    },
  },
  { title: '序号', type: 'index', align: 'center' },
  { title: '账号名称', key: 'target', align: 'center' },
  /* { title: '稿件名称', key: '', align: 'center' },
  { title: '稿件链接', key: '', align: 'center' }, */
  {
    title: '超时督办',
    align: 'center',
    render(h, params) {
      const list = params.row.list || []
      /* 有一条数据没督办就不禁用 */
      const able = list.some((it) => !it.supervise)
      return (
        <i-button
          type="primary"
          disabled={!able}
          nativeOnClick={() => {
            vm.handleSupervise(params.row)
          }}
        >
          {!able ? '已督办' : '督办'}
        </i-button>
      )
    },
  },
]

export const getColumns = (columns, width = {}, source) => {
  const pushColumns = []
  columns.forEach((c) => {
    const _c = {
      title: c.name,
      key: c.field,
      align: 'center',
      width: width[c.field] || '',
      render(h, params) {
        const content = params.row.content
        if (c.field === 'title' && source === 'overview') {
          const title = (content && content[params.column.key]) || params.row[params.column.key] || ''
          return (
            <span class="pointer du-ellipsis" title={title} onClick={() => overviewToDetails(params.row)}>
              {title}
            </span>
          )
        }
        /* 数据中 content 为对象，且无法确定 有多少数据在 content 中 */
        if (content && content[params.column.key]) {
          return h(
            'span',
            {
              class: 'du-ellipsis',
              attrs: {
                title: content[params.column.key],
              },
            },
            content[params.column.key],
          )
        } else {
          return h(
            'span',
            {
              class: 'du-ellipsis',
              attrs: {
                title: params.row[params.column.key] || '',
              },
            },
            params.row[params.column.key] || '',
          )
        }
      },
    }
    pushColumns.push(_c)
  })
  return pushColumns
}

const overviewToDetails = (row) => {
  // console.log(123, row)
  setEntryManuEffect(row.content)
  ROUTER.push({ name: 'directiveManuEffectAssess', query: { id: row.id } })
}
